package org.example.managementsystem.controller;

import org.example.managementsystem.model.Product;
import org.example.managementsystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品控制器
 */
@RestController
@RequestMapping("/api/products")
@CrossOrigin(origins = "*") // 允许跨域请求
public class ProductController {
    private final ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 获取所有产品
     */
    @GetMapping
    public ResponseEntity<List<Product>> getAllProducts() {
        List<Product> products = productService.getAllProducts();
        return ResponseEntity.ok(products);
    }

    /**
     * 根据ID获取产品
     */
    @GetMapping("/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable Long id) {
        Product product = productService.getProductById(id);
        if (product != null) {
            return ResponseEntity.ok(product);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 搜索产品
     */
    @GetMapping("/search")
    public ResponseEntity<List<Product>> searchProducts(@RequestParam String keyword) {
        List<Product> products = productService.searchProducts(keyword);
        return ResponseEntity.ok(products);
    }

    /**
     * 添加产品
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addProduct(@RequestBody Product product) {
        Long id = productService.addProduct(product);
        Map<String, Object> response = new HashMap<>();
        response.put("id", id);
        response.put("message", "产品添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新产品
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, String>> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        boolean updated = productService.updateProduct(product);
        
        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "产品更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "产品更新失败，未找到指定ID的产品");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 删除产品
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, String>> deleteProduct(@PathVariable Long id) {
        boolean deleted = productService.deleteProduct(id);

        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "产品删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "产品删除失败，未找到指定ID的产品");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }
    
    /**
     * 批量更新空产品身份码
     */
    @PostMapping("/batch-update-codes")
    public ResponseEntity<Map<String, Object>> batchUpdateEmptyProductCodes() {
        int updatedCount = productService.batchUpdateEmptyProductCodes();
        
        Map<String, Object> response = new HashMap<>();
        response.put("updatedCount", updatedCount);
        response.put("message", "已更新 " + updatedCount + " 个产品的身份码");
        
        return ResponseEntity.ok(response);
    }
}